<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录</view>
			<input class="u-border-bottom" type="text" v-model="username" placeholder="请输入帐号" />
			<view class="tips"></view>
			<input class="u-border-bottom"  type="text" v-model="password" password="true"placeholder="请输入密码" />
			<view class="tips">{{message}}</view>
			<button @tap="submit" type="primary" >登录</button>
			<view class="alternative">
				<view class="password">忘记密码</view>
				<view class="issue">遇到问题</view>
			</view>
		</view>
		<view class="buttom">
			<view class="loginType">
				<view class="wechat item" >
					<!-- #ifdef MP-WEIXIN -->
					<button  open-type="getUserInfo" class="open-button icon" @getuserinfo="getuserinfo" ><u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon></button>
					<!-- #endif -->
					<!-- #ifdef APP-PLUS -->
					<view class="icon" @click="loginByWeixin"><u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon></view>
					<!-- #endif -->
					
					微信
				</view>
				<view class="QQ item">
					<view class="icon"><u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon></view>
					QQ
				</view>
			</view>
			<view class="hint">
				登录代表同意
				<text class="link">用户协议、隐私政策，</text>
				并授权使用您的账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
	</view>
</template>


<script>
	import ChatLogin from '@/common/mixins/chat_login.js'
	export default {
		mixins: [ChatLogin],

		components: {},

		props: {},

		data() {
			return {
				username:'',
				password:'',
				message:'未注册的账号验证后自动创建'
			}
		},

		computed: {},

		watch: {},
		
		onLoad(e){
			const n=this.$u.random(1, 5)
			this.username='user0'+n
			this.password='user0'+n
			//if()
		},
		created() {},

		mounted() {},

		destroyed() {},

		methods: {
			submit(){
				if(!this.verification())return;
				this.registerOrLogin()
				.then(res=>{
					if(res.code!==0){
						return  Promise.reject(res);
					}
					this.setStorageSync(res)
				}).catch(res=>{
					console.log(res)
					uni.showModal({
						showCancel: false,
						content: res.result.message
					})
				});
			},
			
			registerOrLogin() {
				let params={
					username: this.username,
					password: this.password,
					needPermission: true
				}
				return this.$request('login/registerOrLogin',params)
			},
			
			verification(){
				const username=this.username,password=this.password
				if(!username || !password ){
					this.message='请输帐号和密码'
					return false;
				}
				if(username.length<6){
					this.message='账号须大于六位'
					return false;
				}
				
				if(username.length>20){
					this.message='账号须小于二十位'
					return false;
				}
				if(password.length<6){
					this.message='密码须大于六位'
					return false;
				}
				if(password.length>20){
					this.message='密码须小于二十位'
					return false;
				}
				if(username.indexOf(" ") !==-1 || password.indexOf(" ") !==-1){
					this.message='用户名或密码不能带空格'
					return false;
				}
				if(!this.$u.test.letter(username[0])  && !this.$u.test.chinese(username[0])){
					this.message='用户名必须以字母或汉字开头'
					return false;
				}
				this.message='未注册的账号验证后自动创建'
				return 1;
			}
		}
	};
</script>


<style lang="scss" scoped>
.open-button{
	padding: 0;
	border:none;
	line-height:1;
	position:static;
	background: none;
}
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
	.buttom {
		.loginType {
			display: flex;
			padding: 150rpx 150rpx 150rpx 150rpx;
			justify-content:space-between;
			
			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: $u-content-color;
				font-size: 28rpx;
			}
		}
		
		.hint {
			padding: 20rpx 40rpx;
			font-size: 20rpx;
			color: $u-tips-color;
			
			.link {
				color: $u-type-warning;
			}
		}
	}
}
</style>

